<template>
    <div class="login_container">
        <el-row>
            <el-col :span="12" :xs="0"></el-col>
            <el-col :span="12" :xs="24">
                <el-form :model="loginForm" :rules="rules" class="login_form" ref="loginForms">
                    <h1>Hello</h1>
                    <h2>欢迎来到硅谷甄选</h2>
                    <el-form-item prop="username">
                        <el-input :prefix-icon="User" v-model="loginForm.username"></el-input>
                    </el-form-item>
                    <el-form-item prop="password">
                        <el-input show-password :prefix-icon="Lock" type="password"
                            v-model="loginForm.password"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button :loading="loadingBtn" class="login_btn" type="primary" size="default"
                            @click="login">登录</el-button>
                    </el-form-item>
                </el-form>
            </el-col>
        </el-row>
    </div>
</template>

<script setup lang="ts">
import { Lock, User } from '@element-plus/icons-vue';
import { reactive, ref } from 'vue';
import useUserStore from '../../store/modules/user'
import { useRouter, useRoute } from 'vue-router'
import { ElNotification } from 'element-plus'
import type { FormRules } from 'element-plus'

interface RuleForm {
    username: string
    password: string
}

const rules = reactive<FormRules<RuleForm>>({
    username: [
        { required: true, message: '用户名不为空', trigger: 'blur' },
        { min: 1, max: 10, message: '账号长度在 6 到 10', trigger: 'blur' }
    ],
    password: [
        {
            required: true,
            message: '密码不为空',
            trigger: 'blur',
        }
    ]
})

let loginForms = ref()

const loginForm = reactive<RuleForm>({
    username: 'admin',
    password: '111111'
})

let loadingBtn = ref(false)

let useStore = useUserStore()

let $router = useRouter()

let $route = useRoute()

const login = async () => {
    await loginForms.value.validate()
    loadingBtn.value = true
    //也可以使用.then来做登录成功之后的回调
    try {
        await useStore.userLogin(loginForm)
        //成功时候路由跳转
        let redirect: any = $route.query.redirect
        $router.push({ path: redirect || '/' })
        ElNotification({
            type: 'success',
            message: '欢迎回来!',
            title: 'Hi,今天好啊'
        })
        loadingBtn.value = false
    } catch (error) {
        loadingBtn.value = false
        ElNotification({
            type: 'error',
            message: (error as Error).message
        })
    }
}


</script>

<style scoped>
.login_container {
    width: 100%;
    height: 100vh;
    background: url('../../assets/imges/bg.png') no-repeat;
    background-size: cover;

    .login_form {
        position: relative;
        width: 80%;
        top: 40vh;
        padding: 40px;
        background: url('../../assets/imges/login_bg.png') no-repeat;
        background-size: cover;

        h1 {
            color: white;
            font-size: 40px;
        }

        h2 {
            font-size: 20px;
            color: white;
            margin: 20px 0px;
        }

        .login_btn {
            width: 100%;
        }
    }
}
</style>